package com.cskaoyan.connectionpool;

import com.cskaoyan.util.JDBCUtils;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * 创建日期: 2022/07/27 11:48
 *
 * @author ciggar
 *
 *
 *  V1:
 * 自己实现的数据库连接池
 *
 * V2:
 * 扩容功能
 *
 * V3:
 * + 去掉魔法值
 * + 可配置
 *
 * V4:
 * 实现连接池接口
 */
public class MyConnectionPoolV4 implements DataSource {

    // 维护一个容器 这个容器里面放的是连接对象
    // 从头部存，从尾部取
    static LinkedList<Connection> pool;

    static int INIT_SIZE = 10;
    static int MIN_SIZE = 2;
    static int INCREMENT = 5;

    static {

        try {
            // 读取配置文件
            Properties properties = new Properties();
            properties.load(new FileInputStream("pool.properties"));

            String initSize = properties.getProperty("init_size");
            String minSize = properties.getProperty("min_size");
            String increment = properties.getProperty("increment");

            if (initSize != null && initSize.length() > 0) {
                INIT_SIZE = Integer.valueOf(initSize);
            }
            if (minSize != null && minSize.length() > 0) {
                MIN_SIZE = Integer.valueOf(minSize);
            }
            if (increment != null && increment.length() > 0) {
                INCREMENT = Integer.valueOf(increment);
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("读取配置文件失败！");
        }



        // 初始化数据库连接池

        // 1. 创建实例对象
        pool = new LinkedList<>();

        // 2. 给连接池添加连接
        addCapacity(INIT_SIZE);

    }



    // 获取连接
    public Connection getConnection(){

        // 判断是否需要扩容
        if (pool.size() < MIN_SIZE) {
            addCapacity(INCREMENT);
        }


        Connection connection = pool.removeLast();
        return connection;
    }




    // 回收连接
    public static void recyleConnection(Connection connection){
        pool.addFirst(connection);
    }



    // 增加容量
    private static void addCapacity(int nums){

        if (pool == null) return;
        for (int i = 0; i < nums; i++) {
            Connection connection = JDBCUtils.getConnection();
            pool.addFirst(connection);
        }
    }





    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }
}
